<template>
  <uni-nav-bar title="加注订单" left-icon="left" @clickLeft="navBack" :fixed="true" statusBar></uni-nav-bar>
  <view class="package datas" style="padding:10rpx 20rpx;">
    <uni-datetime-picker v-model="range" type="daterange" :border="false" :clear-icon="false" rangeSeparator="至"
      @change="change" />
  </view>
  <view class="package">
    <view class="space-between">
      <view>加注金额</view>
      <view>{{price}}元</view>
    </view>
    <view class="space-between top">
      <view>加注量</view>
      <view>{{count}}公斤</view>
    </view>
    <view class="space-between top">
      <view>单数</view>
      <view>{{total}}单</view>
    </view>
  </view>
  <scroll-view scroll-y="true" enable-back-to-top @scrolltolower="loadMore" class="scroll-box"
    :style="{ height: pageHeight + 'px' }">
    <view class="package" v-for="(item,index) in orderLists" :key="index" :style="{marginTop:index===0?'0px':'15rpx'}">
      <view class="space-between">
        <view>订单{{item.order_sn}}({{item.order_id}})</view>
        <view>{{item.fill_name}}</view>
      </view>
      <view class="space-between top">
        <view>{{item.driver_name}}({{item.driver_id}})</view>
        <view>{{item.amount}}公斤</view>
      </view>
      <view class="space-between top">
        <view>{{item.price}}元/公斤</view>
        <view>实付金额：{{item.money}}</view>
      </view>
      <view class="space-between top">
        <view>{{item.createtime}}</view>
        <view>{{item.fill_name}}({{item.fill_id}})</view>
      </view>
    </view>
  </scroll-view>
</template>

<script setup lang="ts">
  import { navBack } from '@/utils/navigator';
  import { ref } from 'vue';
  import { toPublish } from '@mqtt';
  import { fill_order_list } from '@/gql/administrator';
  import { showLoading } from '@/utils/prompt';
  import { format, subDays } from 'date-fns';
  import { information } from '@/stores/administrator';
  const TeamInfo = information();
  let now = new Date();
  let end = format(now, 'yyyy-MM-dd');
  let daysAgo = subDays(now, 6);
  let start = format(daysAgo, 'yyyy-MM-dd');
  const range = ref([start, end]);
  const pageHeight = ref();
  const lastPage = ref(1);
  const currentPage = ref(1);
  const orderLists = ref([]);
  const count = ref('')
  const price = ref('')
  const total = ref('')
  uni.getSystemInfo({
    success: function (res) {
      pageHeight.value = res.windowHeight - 220;
    },
  });
  init()
  function init() {
    showLoading()
    const payload = {
      query: fill_order_list,
      variables: {
        page: currentPage.value,
        start,
        end,
        customers_id: Number(TeamInfo.KeyCustom.id)
      },
    };
    toPublish(
      'ql/customers/getOrderFillList',
      payload,
      (obj : any) => {
        const { fill_order_list } = obj.data;
        orderLists.value = [...orderLists.value, ...fill_order_list.list];
        lastPage.value = fill_order_list.lastpage;
        count.value = fill_order_list.count;
        price.value = fill_order_list.price;
        total.value = fill_order_list.total
      }
    );
  }
  /**
   * 加载更多
   */
  function loadMore() {
    if (currentPage.value < lastPage.value) {
      currentPage.value++;
      init();
    }
  }
  /**
   * 时间筛选
   * @param sta  开始时间
   * @param en  结束时间
   */
  function change([sta, en]) {
    start = sta;
    end = en;
    currentPage.value = 1
    orderLists.value = []
    init()
  }
</script>

<style scoped lang="less">
  .top {
    margin-top: 10rpx;
  }
</style>
<style>
  .datas>>>.uni-date__x-input {
    font-size: 30rpx !important;
  }

  .datas>>>.uni-date-x {
    font-size: 30rpx !important;
  }

  .datas>>>.uni-icons {
    font-size: 48rpx !important;
  }
</style>